package com.tanguy.driver.ui.account.activity

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.widget.EditText
import cn.sinata.xldutils_kotlin.activity.SelectPhotoDialog
import com.tanguy.driver.App.Companion.app
import com.tanguy.driver.Const
import com.tanguy.driver.R
import com.tanguy.driver.base.ImageViewTang
import com.tanguy.driver.base.ImmersionTitleActivity
import com.tanguy.driver.bean.DriverRegister
import com.tanguy.driver.net.http.HttpManager
import com.tanguy.driver.ui.window.DatePickDialog
import com.tanguy.driver.ui.window.MessageWindow
import com.tanguy.driver.ui.window.OnePickerViewPopupWindow
import com.tanguy.driver.ui.window.TwoPickerViewPopupWindow
import com.tanguy.driver.utlis.extend.*
import com.tanguy.user.utlis.extend.click
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivityForResult


/**
 * Created by dab on 2017/10/10 0010 15:05
 * 司机加盟
 */
class RegisterSecondActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_driver_join
    private val mDriverRegister by lazy {
        app.driverRegister
    }
    override fun initView() {
        super.initView()
        title = "提交资料"
        if (!TextUtils.isEmpty(mDriverRegister.sexString)) {
            setTextRight(R.id.ttv_sex,mDriverRegister.sexString,R.color.color_333333)
        }
        if (!TextUtils.isEmpty(mDriverRegister.drivage)) {
            setTextRight(R.id.ttv_driving_years,mDriverRegister.drivage,R.color.color_333333)
        }
        if (!TextUtils.isEmpty(mDriverRegister.name)) {
            find<EditText>(R.id.et_name).setText(mDriverRegister.name)
        }
        if (!TextUtils.isEmpty(mDriverRegister.idcard)) {
            find<EditText>(R.id.et_id_number).setText(mDriverRegister.idcard)
        }
        if (!TextUtils.isEmpty(mDriverRegister.drivage)) {
            find<ImageViewTang>(R.id.iv_driving_licence).setImageByFile(mDriverRegister.licence)
        }
        if (!TextUtils.isEmpty(mDriverRegister.travel)) {
            find<ImageViewTang>(R.id.iv_driving_license).setImageByFile(mDriverRegister.travel)
        }
        if (!TextUtils.isEmpty(mDriverRegister.insurance)) {
            find<ImageViewTang>(R.id.iv_car_insurance).setImageByFile(mDriverRegister.insurance)
        }
        if (!TextUtils.isEmpty(mDriverRegister.peoplecar)) {
            find<ImageViewTang>(R.id.iv_group_photo).setImageByFile(mDriverRegister.peoplecar)
        }
        if (!TextUtils.isEmpty(mDriverRegister.modelString)) {
            setTextRight(R.id.ttv_vehicle_type,mDriverRegister.modelString,R.color.color_333333)
        }
        if (!TextUtils.isEmpty(mDriverRegister.careful)) {
            setTextRight(R.id.ttv_examined_time,mDriverRegister.careful,R.color.color_333333)
        }
        if (!TextUtils.isEmpty(mDriverRegister.color)) {
            setTextRight(R.id.ttv_color,mDriverRegister.color,R.color.color_333333)
        }
        if (!TextUtils.isEmpty(mDriverRegister.seat)) {
            setTextRight(R.id.ttv_seat,mDriverRegister.seat,R.color.color_333333)
        }
        if (!TextUtils.isEmpty(mDriverRegister.plateArea)) {
            setText(R.id.tv_plate_number, mDriverRegister.plateArea)
        }
        if (!TextUtils.isEmpty(mDriverRegister.plateNumber)) {
            setText(R.id.et_plate_number, mDriverRegister.plateNumber)
        }
    }

    override fun initEvent() {
        super.initEvent()
        //提交
        click(R.id.btn_submit) {
            setEditText()
            if (!checkDriverRegister(mDriverRegister)) return@click
            registerDriver(mDriverRegister)
        }
        //性别
        click(R.id.ttv_sex) {
            showPopupWindow(OnePickerViewPopupWindow(this, arrayListOf("男", "女")) { _, msg ->
                mDriverRegister.sexString = msg
                setTextRight(R.id.ttv_sex,msg!!,R.color.color_333333)
            })
        }
        //选择颜色
        click(R.id.ttv_color) {
            showPopupWindow(OnePickerViewPopupWindow(this, arrayListOf("白色", "红色", "黑色", "银色", "金色", "灰色", "蓝色", "黄色", "绿色", "其他色")) { _, msg ->
                mDriverRegister.color = msg
                setTextRight(R.id.ttv_color,msg!!,R.color.color_333333)
            })
        }
        //选择座位数
        click(R.id.ttv_seat) {
            showPopupWindow(OnePickerViewPopupWindow(this, arrayListOf("5", "7")) { _, msg ->
                mDriverRegister.seat = msg
                setTextRight(R.id.ttv_seat,msg!!,R.color.color_333333)
            })
        }
        //选择车牌号
        click(R.id.tv_plate_number) {
            val array = arrayListOf("京", "津", "沪", "渝", "冀", "豫", "云", "辽", "黑", "湘", "皖", "鲁", "新", "苏", "浙", "赣", "鄂", "桂", "甘", "晋", "蒙", "陕", "吉", "闽", "贵", "粤", "青", "藏", "川", "宁", "琼")
            val array1 = arrayListOf("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
            showPopupWindow(TwoPickerViewPopupWindow(this, array, array1) { _, oneMsg, _, twoMsg ->
                mDriverRegister.plateArea = oneMsg + twoMsg
                setText(R.id.tv_plate_number, oneMsg + twoMsg)
            })
        }
        //驾龄
        click(R.id.ttv_driving_years) {
            DatePickDialog().apply {
                show(supportFragmentManager, "da")
                setOnDateSetListener { year, month, day ->
                    setTextRight(R.id.ttv_driving_years,"$year-$month-$day",R.color.color_333333)
                    mDriverRegister.drivage = "$year-$month-$day"
                }
            }
        }
        //年审时间
        click(R.id.ttv_examined_time) {
            DatePickDialog().apply {
                show(supportFragmentManager, "ye")
                setOnDateSetListener { year, month, day ->
                    setTextRight(R.id.ttv_examined_time,"$year-$month-$day",R.color.color_333333)
                    mDriverRegister.careful = "$year-$month-$day"
                }
            }
        }
        //选择车型
        click(R.id.ttv_vehicle_type) {
            startActivityForResult<VehicleTypeActivity>(Const.RequestCode.VEHICLE_TYPE)
        }
        //驾驶证
        click(R.id.iv_driving_licence) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.DRIVING_LICENCE)
        }
        //行驶证
        click(R.id.iv_driving_license) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.DRIVING_LICENSE)
        }
        //车险
        click(R.id.iv_car_insurance) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.CAR_INSURANCE)
        }
        //人车合影
        click(R.id.iv_group_photo) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.GROUP_PHOTO)
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.DRIVING_LICENCE && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_driving_licence).setImageByFile(path)
                mDriverRegister.licence = path
            }
            if (requestCode == Const.RequestCode.DRIVING_LICENSE && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_driving_license).setImageByFile(path)
                mDriverRegister.travel = path
            }
            if (requestCode == Const.RequestCode.CAR_INSURANCE && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_car_insurance).setImageByFile(path)
                mDriverRegister.insurance = path
            }
            if (requestCode == Const.RequestCode.GROUP_PHOTO && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_group_photo).setImageByFile(path)
                mDriverRegister.peoplecar = path
            }
            if (requestCode == Const.RequestCode.VEHICLE_TYPE && data != null) {
                val modelId = data.getStringExtra(Const.IntentCode.VEHICLE_TYPE)
                val modelString = data.getStringExtra(Const.IntentCode.VEHICLE_STRING)
                setTextRight(R.id.ttv_vehicle_type,modelString,R.color.color_333333)
                mDriverRegister.modelId = modelId
                mDriverRegister.modelString = modelString
            }
        }
    }

    private fun setEditText() {
        mDriverRegister.name = getEditTextString(R.id.et_name)
        mDriverRegister.idcard = getEditTextString(R.id.et_id_number)
        mDriverRegister.plateNumber = getEditTextString(R.id.et_plate_number)
    }

    private fun registerDriver(driverRegister: DriverRegister) {
        loge(driverRegister.toString())
        HttpManager
                .uploadFile(this, driverRegister.travel)
                .flatMap {
                    loge(it)
                    driverRegister.travelUrl = it
                    HttpManager.uploadFile(this, driverRegister.insurance)
                }
                .flatMap {
                    loge(it)
                    driverRegister.insuranceUrl = it
                    HttpManager.uploadFile(this, driverRegister.peoplecar)
                }
                .flatMap {
                    loge(it)
                    driverRegister.peoplecarUrl = it
                    HttpManager.uploadFile(this, driverRegister.licence)
                }
                .flatMap {
                    loge(it)
                    driverRegister.licenceUrl = it
                    HttpManager.driverRegister(driverRegister)
                }
                .requestSucceed(this) {
                    showPopupWindow(MessageWindow(this, "您已成功提交审核，我们将尽快与你联系，请保持电话通畅！", false) {
                        setResult(Activity.RESULT_OK)
                        finish()
                    })
                }
    }

    override fun onDestroy() {
        super.onDestroy()
        setEditText()
    }
}

